const demo = {
    id: String,
    address: String,

}

class City {
    constructor(name, index) {
        this.name = name;
        this.index = index;
    }
}

const cities = [
    new City('深圳市', 28925),
    new City('广州市', 28805),
    new City('东莞市', 12367),
    new City('佛山市', 10365),
    new City('惠州市', 9312),
    new City('中山市', 8885),
    new City('珠海市', 8328),
    new City('汕头市', 7728),
    new City('江门市', 6553),
    new City('湛江市', 5321),
    new City('肇庆市', 5061),
    new City('揭阳市', 4783),
    new City('清远市', 4669),
    new City('潮州市', 3556),
    new City('韶关市', 3279),
    new City('茂名市', 3110),
    new City('汕尾市', 2609),
    new City('阳江市', 2344),
    new City('云浮市', 1909),
    new City('河源市', 1773)
];
/**
 * 定义数据集，data 包含用户 id、地区和喜欢的物品
 * @type {Array<>}
 */
const data = [
    { id: 1, area: '北京', item: 'A' },
    { id: 2, area: '上海', item: 'B' },
    { id: 3, area: '北京', item: 'C' },
    { id: 4, area: '广州', item: 'D' },
    { id: 5, area: '上海', item: 'E' },
    { id: 6, area: '深圳', item: 'F' },
    { id: 7, area: '重庆', item: 'G' },
    { id: 8, area: '广州', item: 'H' },
    { id: 9, area: '北京', item: 'I' },
    { id: 10, area: '上海', item: 'J' }
];

// 根据地区划分数据集
const areaData = {};
for (let i = 0; i < data.length; i++) {
    const area = data[i].area;
    const item = data[i].item;
    if (!(area in areaData)) {
        areaData[area] = new Set();
    }
    areaData[area].add(item);
}

// 构建物品-地区矩阵
const items = Array.from(new Set(data.map(d => d.item)));
const areas = Object.keys(areaData);
const itemAreaMatrix = [];
for (let i = 0; i < items.length; i++) {
    const row = new Array(areas.length).fill(0);
    for (let j = 0; j < areas.length; j++) {
        if (areaData[areas[j]].has(items[i])) {
            row[j] = 1;
        }
    }
    itemAreaMatrix.push(row);
}

// 计算地区间相似度
const areaSimMatrix = cosineSimilarity(transpose(itemAreaMatrix));

// 定义余弦相似度计算方法
function cosineSimilarity(matrix) {
    const dotProduct = (v1, v2) => v1.map((_, i) => v1[i] * v2[i]).reduce((acc, curr) => acc + curr, 0);
    const len = v => Math.sqrt(v.map(x => x * x).reduce((acc, curr) => acc + curr, 0));
    return matrix.map((row, i) => {
        return matrix.map((_, j) => {
            return dotProduct(row, matrix[j]) / (len(row) * len(matrix[j]));
        });
    });
}

// 根据用户所在地区推荐物品
function RecommendItems(userArea) {
    const areaIndex = areas.indexOf(userArea);
    const simScores = areaSimMatrix[areaIndex];
    const sortedSimScores = simScores.map((score, index) => [index, score])
        .sort((a, b) => b[1] - a[1]);
    const simIndices = sortedSimScores.slice(1, 6).map(s => s[0]); // 返回相似度较高的5个地区
    const recommendedItems = new Set(); // 存储推荐结果
    for (let i = 0; i < simIndices.length; i++) {
        const area = areas[simIndices[i]];
        areaData[area].forEach(item => recommendedItems.add(item));
    }
    return Array.from(recommendedItems);
}

// 转置矩阵
function transpose(matrix) {
    return matrix[0].map((_, i) => matrix.map(row => row[i]));
}
